package com.es.disped.web.controller.syllabus;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.es.disped.api.syllabus.SyllabusService;
import com.es.disped.common.constant.RecordStatus;
import com.es.disped.common.model.MsgModel;
import com.es.disped.common.model.PageModel;
import com.es.disped.core.controller.BaseController;
import com.es.disped.dao.model.Syllabus;
import com.es.disped.dao.model.SyllabusExample;
import com.es.disped.dao.model.TemplateForeignModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(value="教学大纲管理")
@Controller
@RequestMapping("/manage/syllabus")
public class SyllabusController extends BaseController{

	private final String TEMPLATE_NAMESPACE="com.es.disped.dao.mapper.template.TemplateCustomMapper";
	
	private final String SYLLABUS_NAMESPACE="com.es.disped.dao.mapper.syllabus.SyllabusCustomMapper";
	
	@Autowired
	SyllabusService syllabusService;
	
	@ApiOperation(value="新增教学大纲",response=MsgModel.class,httpMethod="POST",notes="添加教学大纲")
	@RequiresPermissions(value={"admin:save","sys:save"},logical=Logical.OR)
	@Transactional
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public @ResponseBody MsgModel save(@RequestBody @ApiParam(name="syllabus",value="教学大纲对象",required=true) Syllabus syllabus)
	{
		int count=0;
		if(Optional.of(syllabus)
				.filter(name->syllabus.getSyllabusName()!=null)
				.filter(tpId->syllabus.getTemplateId()!=null)
				.filter(address->syllabus.getAddress()!=null)
				.filter(bday->syllabus.getSyllabusBeginDay()!=null)
				.filter(eday->syllabus.getSyllabusEndDay()!=null)
				.filter(thway->syllabus.getTeachWay()!=null)
				.filter(redate->syllabus.getReminderTime()!=null)
				.isPresent())
		{
			syllabus.setSyllabusId(getUUID());
			TemplateForeignModel foreignModel=this.getService().selectOne(
					TEMPLATE_NAMESPACE+".getTemplateForeignModel", syllabus.getTemplateId());
			syllabus.setTemplateName(foreignModel.getTemplateName());
			syllabus.setSyllabusValid(RecordStatus.USEAGE.getValue());
			count=syllabusService.insertSelective(syllabus);
		}
		return this.resultMsg(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"保存成功":"保存失败",count==1?syllabus:"");
	}
	
	
	@ApiOperation(value="更新教学大纲",response=MsgModel.class,httpMethod="POST",notes="更新教学大纲")
	@RequiresPermissions(value={"admin:update","sys:update"},logical=Logical.OR)
	@Transactional
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public @ResponseBody MsgModel update(@RequestBody @ApiParam(name="syllabus",value="教学大纲对象",required=true) Syllabus syllabus)
	{
		int count=0;
		if(!this.isNull(syllabus.getSyllabusId()))
		{
			if(!this.isNull(syllabus.getTemplateId()))
			{
				TemplateForeignModel foreignModel=this.getService().selectOne(
						TEMPLATE_NAMESPACE+".getTemplateForeignModel", syllabus.getTemplateId());
				syllabus.setTemplateName(foreignModel.getTemplateName());
			}
			count=syllabusService.updateByPrimaryKeySelective(syllabus);
		}
		return this.resultMsg(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"更新成功":"更新失败",count==1?syllabus:"");
	}
	
	
	@ApiOperation(value="删除教学大纲",response=MsgModel.class,httpMethod="POST",notes="删除教学大纲")
	@RequiresPermissions(value={"admin:delete","sys:delete"},logical=Logical.OR)
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public @ResponseBody MsgModel delete(@RequestBody @ApiParam(name="syllabus",value="教学大纲对象",required=true) Syllabus syllabus)
	{
		int count=0;
		if(!this.isNull(syllabus.getSyllabusId()))
		{
			count=this.getService().delete(SYLLABUS_NAMESPACE+".deleteSyllabusModel", syllabus.getSyllabusId());
		}
		return this.resultMsg(count>=1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count>=1?"删除成功":"删除失败",count>=1?syllabus:"");
	}
	
	
	@ApiOperation(value="检索教学大纲",response=MsgModel.class,httpMethod="POST",
			notes="检索教学大纲，精确检索：主键，课程模板ID，开始及截止日期；模糊检索：大纲名称，课程模板名称，上课地址，教学类型")
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public @ResponseBody MsgModel list(@RequestBody @ApiParam(name="syllabus",value="教学大纲对象",required=false) Syllabus syllabus)
	{
		SyllabusExample listExample=new SyllabusExample();
		SyllabusExample.Criteria criteria=listExample.createCriteria();
		if(!this.isNull(syllabus.getSyllabusId()))
		{
			criteria.andSyllabusIdEqualTo(syllabus.getSyllabusId());
		}
		if(!this.isNull(syllabus.getSyllabusName()))
		{
			criteria.andSyllabusNameLike("%"+syllabus.getSyllabusName()+"%");
		}
		if(!this.isNull(syllabus.getTemplateId()))
		{
			criteria.andTemplateIdEqualTo(syllabus.getTemplateId());
		}
		if(!this.isNull(syllabus.getTemplateName()))
		{
			criteria.andTemplateNameLike("%"+syllabus.getTemplateName()+"%");
		}
		if(!this.isNull(syllabus.getAddress()))
		{
			criteria.andAddressLike("%"+syllabus.getAddress()+"%");
		}
		if(!this.isNull(syllabus.getSyllabusBeginDay()))
		{
			criteria.andSyllabusBeginDayGreaterThanOrEqualTo(syllabus.getSyllabusBeginDay());
		}
		if(!this.isNull(syllabus.getSyllabusEndDay()))
		{
			criteria.andSyllabusEndDayLessThanOrEqualTo(syllabus.getSyllabusEndDay());
		}
		if(!this.isNull(syllabus.getTeachWay()))
		{
			criteria.andTeachWayLike("%"+syllabus.getTeachWay()+"%");
		}
		List<Syllabus> list=syllabusService.selectByExample(listExample);
		return this.resultMsg(!this.isNull(list)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(list)?"加载成功":"查询失败",!this.isNull(list)?list:"");
	}
	
	@ApiOperation(value="分页加载教学大纲",response=MsgModel.class,httpMethod="POST",notes="分页加载教学大纲，分页参数['offset','limit']")
	@RequestMapping(value="/pages",method=RequestMethod.POST)
	public @ResponseBody MsgModel pages(@RequestBody @ApiParam(name="paramMap",value="分页参数[offset,limit]",required=true) Map<String, Object> paramMap)
	{
		SyllabusExample listExample=new SyllabusExample();
		PageModel<Syllabus> pageModel=null;
		if(Optional.of(paramMap)
				.filter(offset->paramMap.containsKey("offset")!=false)
				.filter(limit->paramMap.containsKey("limit")!=false)
				.isPresent())
		{
			pageModel=syllabusService.selectByExampleForOffsetPage(listExample,
					Integer.valueOf(paramMap.get("offset").toString()), Integer.valueOf(paramMap.get("limit").toString()));
			
		}
		return this.resultMsg(!this.isNull(pageModel)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(pageModel)?"教学大纲加载成功":"教学大纲加载失败",!this.isNull(pageModel)?pageModel:"");
	}
}
